home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / tar-dist / getopt1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-31  |  2.7 KB  |  126 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19.  
  20. #include "getopt.h"
  21.  
  22. int
  23. getopt_long (argc, argv, options, long_options, opt_index)
  24.      int argc;
  25.      char **argv;
  26.      char *options;
  27.      struct option *long_options;
  28.      int *opt_index;
  29. {
  30.   int val;
  31.   _getopt_long_options = long_options;
  32.   val = getopt (argc, argv, options);
  33.   if (val == 0)
  34.     *opt_index = option_index;
  35.   return val;
  36. }
  37.  
  38. #ifdef TEST
  39.  
  40. #include <stdio.h>
  41.  
  42. int
  43. main (argc, argv)
  44.      int argc;
  45.      char **argv;
  46. {
  47.   char c;
  48.   int digit_optind = 0;
  49.  
  50.   while (1)
  51.     {
  52.       int this_option_optind = optind;
  53.       char *name = '\0';
  54.       int option_index = 0;
  55.       static struct option long_options[]
  56.     = {{ "add", 1, 0, 0 },
  57.        { "append", 0, 0, 0 },
  58.        { "delete", 1, 0, 0 },
  59.        { "verbose", 0, 0, 0 },
  60.        { "create", 0, 0, 0 },
  61.        { "file", 1, 0, 0 },
  62.        { 0, 0, 0, 0}};
  63.  
  64.       c = getopt_long (argc, argv, "abc:d:0123456789",
  65.                long_options, &option_index);
  66.       if (c == EOF)
  67.     break;
  68.       if (option_index)
  69.     {
  70.       printf ("option %s", (long_options[option_index]).name);
  71.       if (optarg)
  72.         printf (" with arg %s", optarg);
  73.       printf ("\n");
  74.     }
  75.       else
  76.     switch (c)
  77.       {
  78.       case '0':
  79.       case '1':
  80.       case '2':
  81.       case '3':
  82.       case '4':
  83.       case '5':
  84.       case '6':
  85.       case '7':
  86.       case '8':
  87.       case '9':
  88.         if (digit_optind != 0 && digit_optind != this_option_optind)
  89.           printf ("digits occur in two different argv-elements.\n");
  90.         digit_optind = this_option_optind;
  91.         printf ("option %c\n", c);
  92.         break;
  93.  
  94.       case 'a':
  95.         printf ("option a\n");
  96.         break;
  97.  
  98.       case 'b':
  99.         printf ("option b\n");
  100.         break;
  101.  
  102.       case 'c':
  103.         printf ("option c with value `%s'\n", optarg);
  104.         break;
  105.  
  106.       case '?':
  107.         break;
  108.  
  109.       default:
  110.         printf ("?? getopt returned character code 0%o ??\n", c);
  111.       }
  112.     }
  113.  
  114.   if (optind < argc)
  115.     {
  116.       printf ("non-option ARGV-elements: ");
  117.       while (optind < argc)
  118.     printf ("%s ", argv[optind++]);
  119.       printf ("\n");
  120.     }
  121.  
  122.   return 0;
  123. }
  124.  
  125. #endif /* TEST */
  126.